#/bin/bash
WORK_DIR=$(cd $(dirname $0)/..; pwd)

ARCH=aarch64

#产品ID，根据不同的出包脚本进行配置
OUR_PID=12
OUR_PM=P42PLUS
export CNCPROJNAME=$OUR_PM
export CNCPROJID=$OUR_PID
#pro文件路径
PRO_FILE=$WORK_DIR/winupcnc.pro
BRANCHNAME=master
if [ ! -z "$1" ];then  
   BRANCHNAME=$1
fi
TS_DIR=$WORK_DIR/res/views/linuxts
CMTLOG=commitLog-$BRANCHNAME.txt
QT_PATH=/usr/local/aarch64/Qt5.15.16-aarch64/
export QTDIR=$QT_PATH
#远程保存目录
DST_PATH=/mnt/swap/product/$OUR_PM/cnc-aarch64/
OUT_PATH=$WORK_DIR/output_arm64/release/bin
if [ -z "$BUILDTOOL" ]; then
	BUILDTOOL=$WORK_DIR/../buildtool
fi

LUA_ENCRYPT=$BUILDTOOL/luaencrypt/index.js

GIT_TOOL=git
LAST_TIME=UNKNOW
LAN_CONVERT=$BUILDTOOL/excel2ts/csv2ts.js

func_update_from_git(){
	#从gitlab上更新最新的代码下来
	echo "正在更新源代码..."
	${GIT_TOOL} fetch  --all
	${GIT_TOOL} reset  --hard origin/${BRANCHNAME}
	${GIT_TOOL} pull

	#更新submodule
	${GIT_TOOL} submodule update --init --recursive
}

func_get_last_time(){
	#获取gitlab的最后提交时间
	echo $(${GIT_TOOL} log -1 --format=%ad --date=format:%Y%m%d%H%M)
}

#build Makefile
func_build(){
	echo "正在编译..."
	${QT_PATH}bin/qmake ${PRO_FILE} -spec ${QT_PATH}mkspecs/linux-aarch64-gnu-g++
	/usr/bin/make qmake_all
	make -j4
}

func_copy_res() {
	echo "正在处理资源文件..."
	#处理二进制文件，兼容启动脚本
	mv $OUT_PATH/UPCNC3 $OUT_PATH/UPCNC
	mv $OUT_PATH/UPCNC3.ini $OUT_PATH/UPCNC.ini
	#处理配置
	#处理校准脚本
	#处理第三方文件
}

func_build_ts() {
	echo "正在处理语言文件..."
	
        node $LAN_CONVERT "$TS_DIR/D500Trans.csv" "$TS_DIR"
	return 0
	
	for file in $TS_DIR/*.ts
		do
		QMFILE=`echo  $file | awk -F ".ts" '{print $1}'`.qm
		lrelease $file -qm $QMFILE

		rm -f $file
	done
}

func_pre_package(){
	echo "正在修订版本号..."	
	file=$OUT_PATH/sys.json
	echo $file
	sed -e "s/{time}/${LAST_TIME}/g" $file | sed -e "s/{CNCPROJNAME}/${CNCPROJNAME}/g" >> $file.new
	mv $file.new $file -f
	
	if [ -n "$CI_COMMIT_BRANCH" ]; then
		sed -e "s/{CI_COMMIT_BRANCH}/${CI_COMMIT_BRANCH}/g" $file >> $file.new
		mv $file.new $file -f
	fi
	
	if [ -n "$CI_COMMIT_SHORT_SHA" ]; then
		sed -e "s/{CI_COMMIT_SHORT_SHA}/${CI_COMMIT_SHORT_SHA}/g" $file >> $file.new
		mv $file.new $file -f
	fi

	if [ -n "$CI_COMMIT_TIMESTAMP" ]; then
		sed -e "s/{CI_COMMIT_TIMESTAMP}/${CI_COMMIT_TIMESTAMP}/g" $file >> $file.new
		mv $file.new $file -f
	fi
	
}

func_build_package(){
	#打包成D500_{ARCH}_{LAST_TIME}_{BRANCHNAME}.tar的文件
	echo "正在打包..."
	cd $OUT_PATH
	rm -f ./*.tar
	tar cvf ${OUR_PM}_${ARCH}_${LAST_TIME}_${BRANCHNAME}.tar ./*
	
	cd $WORK_DIR 	
}

func_build_remote(){
	#打包git log到远程目录下
	echo "正在备份提交记录到远程目录..."
	mv $OUT_PATH/*.tar $DST_PATH
	${GIT_TOOL} log --pretty=format:"%ai,%h,%an:%s" -30 > ${DST_PATH}$CMTLOG
	
	cd $WORK_DIR/cncmachine
	echo . >> ${DST_PATH}$CMTLOG
	echo cncmachine >> ${DST_PATH}$CMTLOG
	${GIT_TOOL} log --pretty=format:"%ai,%h,%an:%s" -15 >> ${DST_PATH}$CMTLOG
	
	cd $WORK_DIR/components
	echo . >> ${DST_PATH}$CMTLOG
	echo components >> ${DST_PATH}$CMTLOG
	${GIT_TOOL} log --pretty=format:"%ai,%h,%an:%s" -15 >> ${DST_PATH}$CMTLOG
	
	cd $WORK_DIR/cncbase
	echo . >> ${DST_PATH}$CMTLOG
	echo cncbase >> ${DST_PATH}$CMTLOG
	${GIT_TOOL} log --pretty=format:"%ai,%h,%an:%s" -15 >> ${DST_PATH}$CMTLOG
	
	cd $WORK_DIR
}

function lua_encrypt_to_package()
{
    for file in $1/*.lua; do
        if [ -f "$file" ]; then
            echo "$file"
            name=$(echo "$file" | rev | cut -d '/' -f 1 | rev)
            mkdir -p "$2"
            node $LUA_ENCRYPT --in=$file --out="$2"/"$name" --p=up3d
        fi
    done
}

function encrypt_lua_script()
{
	sp=$WORK_DIR/res/script
	op=$OUT_PATH/script
	lua_encrypt_to_package $sp $op
	lua_encrypt_to_package $sp/$OUR_PID/ $op/$OUR_PID
}

# 脚本主流程开始

if [ -n "$DDTOKEN" ]; then
    func_update_from_git
fi

if [ $? -eq 0 ];then
    echo "git pull success"
else
    echo "**********!!!git pull error!!!************"
    exit 1
fi

LAST_TIME=$(func_get_last_time)

cd $WORK_DIR/app/UPCNC
ln -s ../../res/views views
cd $WORK_DIR

func_build_ts
func_build
if [ $? -eq 0 ];then
    echo "build success"
else
    echo "**********!!!build error!!!************"
    exit 2
fi

func_copy_res
if [ -n "$DDTOKEN" ]; then
    encrypt_lua_script
fi
func_pre_package
func_build_package


if [ -n "$DDTOKEN" ]; then
 func_build_remote
fi